<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="领用单号" prop="receiveId">
        <el-input v-model="queryParams.receiveId" placeholder="请输入领用单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="领用人" prop="assetUser">
        <el-input v-model="queryParams.assetUser" placeholder="请输入领用人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="领用后使用公司" prop="assetCompany">
        <el-input v-model="queryParams.assetCompany" placeholder="请输入领用后使用公司" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="领用后使用部门" prop="assetDepartment">
        <el-input v-model="queryParams.assetDepartment" placeholder="请输入领用后使用部门" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="领用类型" prop="assetType">
        <el-select v-model="queryParams.assetType" placeholder="请选择领用类型" clearable>
          <el-option v-for="dict in dict.type.receive_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="资产编码" prop="assetCode">
        <el-input v-model="queryParams.assetCode" placeholder="请输入资产编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['asset:receive:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['asset:receive:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['asset:receive:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['asset:receive:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receiveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="领用单号" align="center" prop="receiveId" />
      <el-table-column label="领用人" align="center" prop="assetUser" />
      <el-table-column label="领用后使用公司" align="center" prop="assetCompany" />
      <el-table-column label="领用后使用部门" align="center" prop="assetDepartment" />
      <el-table-column label="领用类型" align="center" prop="assetType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.receive_type" :value="scope.row.assetType" />
        </template>
      </el-table-column>
      <el-table-column label="领用说明" align="center" prop="receiveRemark" />
      <el-table-column label="资产编码" align="center" prop="assetCode" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['asset:receive:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['asset:receive:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改领用对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="领用单号" prop="receiveId">
          <el-input v-model="form.receiveId" placeholder="请输入领用单号" />
        </el-form-item>
        <el-form-item label="领用人" prop="assetUser">
          <!-- <el-input v-model="form.assetUser" placeholder="请输入领用人" /> -->
          <el-select v-model="form.assetUser" class="m-2" placeholder="请选择领用人" size="large">
            <el-option v-for="(item, index) in assetUserOptions" :key="index" :label="item.userName"
              :value="item.userName" />
          </el-select>

        </el-form-item>
        <el-form-item label="资产编码" prop="assetCode">
          <el-input v-model="form.assetCode" placeholder="请输入资产编码" />
        </el-form-item>
        <el-form-item label="领用后使用公司" prop="assetCompany">
          <!-- <el-input v-model="form.assetCompany" placeholder="请输入领用后使用公司" /> -->
          <treeselect v-model="form.assetCompany" :options="assetCompanyOptions" :show-count="true"
            placeholder="请选择领用后使用公司" />
        </el-form-item>
        <el-form-item label="领用后使用部门" prop="assetDepartment">
          <!-- <el-input v-model="form.assetDepartment" placeholder="请输入领用后使用部门" /> -->
          <treeselect v-model="form.assetDepartment" :options="deptOptions" :show-count="true" placeholder="请选择领用后使用部门" />
        </el-form-item>
        <el-form-item label="领用类型" prop="assetType">
          <el-select v-model="form.assetType" placeholder="请选择领用类型">
            <el-option v-for="dict in dict.type.receive_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领用说明" prop="receiveRemark">
          <el-input v-model="form.receiveRemark" placeholder="请输入领用说明" />
        </el-form-item>

        <el-divider v-if="!isEdit" content-position="center">资产列表信息</el-divider>

        <el-table v-loading="loading" :data="assetList" v-if="!isEdit">
          <!-- <el-table-column type="selection" width="55" align="center" /> -->
          <el-table-column label="资产编码" align="center" prop="assetCode" />
          <el-table-column label="资产名称" align="center" prop="assetName" />
          <el-table-column label="资产类别" align="center" prop="assetClass">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.asset_class" :value="scope.row.assetClass" />
            </template>
          </el-table-column>
          <el-table-column label="管理员" align="center" prop="assetAdmin" />
          <el-table-column label="使用人" align="center" prop="assetUser" />
          <el-table-column label="购入时间" align="center" prop="assetBuy" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.assetBuy, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="所属公司" align="center" prop="assetCompany" />
          <el-table-column label="使用公司" align="center" prop="assetUseCompany" />
          <el-table-column label="使用部门" align="center" prop="assetDepartment" />
          <el-table-column label="区域" align="center" prop="assetArea">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.asset_area" :value="scope.row.assetArea" />
            </template>
          </el-table-column>
          <el-table-column label="规格型号" align="center" prop="assetModel" />
          <el-table-column label="计量单位" align="center" prop="assetUnit" />
          <el-table-column label="金额" align="center" prop="assetMoney" />
          <el-table-column label="来源" align="center" prop="assetSource">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.asset_source" :value="scope.row.assetSource" />
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="assetRemark" />
          <el-table-column label="资产状态" align="center" prop="assetStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.asset_status" :value="scope.row.assetStatus" />
            </template>
          </el-table-column>
          <el-table-column label="资产照片" align="center" prop="assetImages" width="100">
            <template slot-scope="scope">
              <image-preview :src="scope.row.assetImages" :width="50" :height="50" />
            </template>
          </el-table-column>

        </el-table>

        <pagination v-if="!isEdit" v-show="assettotal > 0" :total="assettotal" v-model:page-size="queryParams2.pageSize"
          :page.sync="queryParams2.pageNum" :limit.sync="queryParams2.pageAssetSize" :page-sizes="queryParams2.pageSizes"
          @pagination="getAssetList" />

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReceive, getReceive, delReceive, addReceive, updateReceive, listAsset, deptTreeSelect, updateAssetRecipient } from "@/api/asset/receive";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { throws } from "assert";

export default {
  name: "Receive",
  dicts: ['asset_area', 'asset_class', 'asset_source', 'asset_status', 'receive_type'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产总条数
      assettotal: 0,
      // 部门树选项
      deptOptions: [],
      //所属公司
      assetCompanyOptions: [],
      //领用后使用部门
      assetUseCompanyOptions: [],
      // 资产列表表格数据
      assetList: [],
      // 领用表格数据
      receiveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 判断是否为修改
      isEdit: false,
      //领用人选项
      assetUserOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pageAssetSize: 5,
        receiveId: null,
        assetUser: null,
        assetCompany: null,
        assetDepartment: null,
        assetType: null,
        assetCode: null,
        recipient: null,
        receiveAssetCode: null,
        recipientAsset: null,
      },
      // 查询参数2
      queryParams2: {
        pageNum: 1,
        pageSize: 5,
        pageAssetSize: 5,
        pageSizes: [5, 10, 20, 50, 100],
        returnId: null,
        returnHandler: null,
        assetCode: null,
        returnTime: null,
        belongsCompany: null,
        assetName: null,
        assetClass: null,
        assetAdmin: null,
        assetUser: null,
        assetBuy: null,
        assetCompany: null,
        assetUseCompany: null,
        assetDepartment: null,
        assetArea: null,
        assetMoney: null,
        assetSource: null,
        assetStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        receiveId: [
          { required: true, message: "领用单号不能为空", trigger: "blur" }
        ],
        assetUser: [
          { required: true, message: "领用人不能为空", trigger: "blur" }
        ],
        assetCompany: [
          { required: true, message: "领用后使用公司不能为空", trigger: "blur" }
        ],
        assetCode: [
          { required: true, message: "资产编码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  watch: {
    open: function (val) {
      !val && (this.isEdit = false)
    }
  },
  methods: {
    /** 查询领用列表 */
    getList() {
      this.loading = true;
      listReceive(this.queryParams).then(response => {
        this.receiveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询资产列表列表 */
    getAssetList() {
      this.loading = true;
      listAsset(this.queryParams).then(response => {
        this.assetList = response.rows;
        this.assettotal = response.total;
        this.loading = false;


      });
    },
    /** 查询使用部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
        this.assetCompanyOptions = response.data;
        this.assetUseCompanyOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        receiveId: null,
        assetUser: null,
        assetCompany: null,
        assetDepartment: null,
        assetType: null,
        receiveRemark: null,
        assetCode: null,
        receiveAssetCode: null,
        recipientAsset: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    async handleAdd() {
      this.reset();
      await getReceive('1').then(response => {
        this.assetUserOptions = response.assetUsers;
        this.open = true;
        this.title = "添加领用";
        this.getAssetList();
        this.getDeptTree();

      })

    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      await getReceive(id).then(response => {
        this.form = response.data;
        this.isEdit = true;
        this.open = true;
        this.title = "修改领用";
        this.assetUserOptions = response.assetUsers;

      });
      getDeptTree();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReceive(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReceive(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              console.log(response);
              var recipient = response.data.recipient;
              console.log(recipient);
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除领用编号为"' + ids + '"的数据项？').then(function () {
        return delReceive(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('asset/receive/export', {
        ...this.queryParams
      }, `receive_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
